package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustableTabbedPane;
import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import java.awt.*;

public class GDCharEditTabbedPane extends AdjustableTabbedPane implements GDUIMasterySupport {
    private GDCharDataOverviewPane pnlCharData;
    private GDCharRiftNShrineTabbedPane pnlRiftShrine;
    private GDCharMasteryPane pnlMastery1;
    private GDCharMasteryPane pnlMastery2;
    private int skillPoints;
    private GDChar gdc;

    public GDCharEditTabbedPane() {
        this.pnlCharData = new GDCharDataOverviewPane(this);
        this.pnlRiftShrine = new GDCharRiftNShrineTabbedPane();
        this.pnlMastery1 = new GDCharMasteryPane(this, false);
        this.pnlMastery2 = new GDCharMasteryPane(this, false);
        this.add(this.pnlCharData);
        this.add(this.pnlRiftShrine);
        this.add(this.pnlMastery1);
        this.add(this.pnlMastery2);
        this.adjustUI();
    }

    @Override
    public void adjustUI() {
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTabbed = UIManager.getDefaults().getFont("TabbedPane.font");
        fntTabbed = fntTabbed.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        this.setFont(fntTabbed);
        int index = 0;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_CHAR_DATA"));
        this.setIconAt(index, GDImagePool.iconTabCharData16);
        ++index;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_SHRINE_RIFT"));
        this.setIconAt(index, GDImagePool.iconTabCharPortal16);
        ++index;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_1"));
        ++index;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_2"));
        ++index;
        this.pnlCharData.adjustUI();
        this.pnlRiftShrine.adjustUI();
        this.pnlMastery1.adjustUI();
        this.pnlMastery2.adjustUI();
    }

    public void setChar(final GDChar gdc) {
        this.gdc = gdc;
        if (gdc == null) {
            this.skillPoints = 0;
        } else {
            this.skillPoints = gdc.getSkillPoints();
        }
        this.pnlCharData.setChar(gdc);
        this.pnlRiftShrine.setChar(gdc);
        this.pnlMastery1.setChar(gdc);
        this.pnlMastery2.setChar(gdc);
        if (gdc == null) {
            this.setTitleAt(2, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_1"));
            this.setTitleAt(3, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_2"));
            this.pnlMastery1.setMastery(null);
            this.pnlMastery2.setMastery(null);
        } else {
            final GDChar.SkillInfo[] infos = gdc.getMasteryInfo();
            if (infos == null) {
                this.setTitleAt(2, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_1"));
                this.setTitleAt(3, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_2"));
                this.pnlMastery1.setMastery(null);
                this.pnlMastery2.setMastery(null);
                return;
            }
            if (infos.length == 1) {
                this.setTitleAt(2, infos[0].name);
                this.setTitleAt(3, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_2"));
                this.pnlMastery1.setMastery(infos[0].id);
                this.pnlMastery2.setMastery(null);
            }
            if (infos.length == 2) {
                this.setTitleAt(2, infos[0].name);
                this.setTitleAt(3, infos[1].name);
                this.pnlMastery1.setMastery(infos[0].id);
                this.pnlMastery2.setMastery(infos[1].id);
            }
        }
    }

    public void updateChar(final GDChar gdc) {
        this.pnlMastery1.updateChar(gdc);
        this.pnlMastery2.updateChar(gdc);
        this.pnlCharData.updateChar(gdc);
        this.pnlRiftShrine.updateChar(gdc);
    }

    public String getCharName() {
        if (this.pnlCharData == null) {
            return null;
        }
        return this.pnlCharData.getCharName();
    }

    public void updateConfig() {
        if (this.pnlCharData != null) {
            this.pnlCharData.updateConfig();
        }
        if (this.pnlMastery1 != null) {
            this.pnlMastery1.updateConfig();
        }
        if (this.pnlMastery2 != null) {
            this.pnlMastery2.updateConfig();
        }
    }

    @Override
    public int getSkillPoints() {
        return this.skillPoints;
    }

    @Override
    public void setSkillPoints(final int points) {
        this.skillPoints = points;
        if (this.pnlCharData != null) {
            this.pnlCharData.setSkillPoints(points);
        }
        if (this.pnlMastery1 != null) {
            this.pnlMastery1.setSkillPoints(points);
        }
        if (this.pnlMastery2 != null) {
            this.pnlMastery2.setSkillPoints(points);
        }
    }

    @Override
    public int getPointsForSkill(final String skillID) {
        int points = this.pnlMastery1.getPointsForSkill(skillID);
        if (points > 0) {
            return points;
        }
        points = this.pnlMastery2.getPointsForSkill(skillID);
        return points;
    }

    @Override
    public int getCharLevel() {
        if (this.gdc == null) {
            return 0;
        }
        return this.gdc.getLevel();
    }

    @Override
    public void resetMastery(final String masteryID) {
        if (masteryID == null) {
            return;
        }
        int points = this.gdc.refundMastery(masteryID);
        if (points > 0) {
            points += this.getSkillPoints();
            this.setSkillPoints(points);
            this.pnlCharData.resetMastery(masteryID);
            this.pnlMastery1.resetMastery(masteryID);
            this.pnlMastery2.resetMastery(masteryID);
        }
    }
}
